//
//  LGBodayStatisticsController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/26.
//  体征统计图
/**  33-血氧、34-血压、 35-心率、 36-体重 */
#import "LGBodayStatisticsController.h"
#import "LGBodayStatisticsOxygenView.h"
#import "LGBodayStatisticsPressureView.h"
#import "LGBodayStatisticsRateView.h"
#import "LGBodayStatisticsWeightView.h"
#import "LGChartDetailAlertView.h"
@interface LGBodayStatisticsController ()<UIScrollViewDelegate,LGBodayStatisticsOxygenDelegate,LGBodayStatisticsPressureDelegate,LGBodayStatisticsRateDelegate,LGBodayStatisticsWeightDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGChartDetailAlertView *alertView;
/**   */
@property (nonatomic , strong)LGBodayStatisticsOxygenView *oxygenView;
/**   */
@property (nonatomic , strong)LGBodayStatisticsPressureView *pressureView;
/**   */
@property (nonatomic , strong)LGBodayStatisticsRateView *rateView;
/**   */
@property (nonatomic , strong)LGBodayStatisticsWeightView *weightView;

@property (nonatomic , strong)NSArray *oxygenArray;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
@property (nonatomic , copy)NSString *startDate30;
@property (nonatomic , copy)NSString *startDate7;
@property (nonatomic , copy)NSString *endDate;

@end

@implementation LGBodayStatisticsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"体征数据统计";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    self.endDate = [self.formatter stringFromDate:[NSDate date]];
    self.startDate7 = [self.formatter stringFromDate:[[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*6]];
    self.startDate30 = [self.formatter stringFromDate:[[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*29]];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.oxygenView];
    [self.scrollView addSubview:self.pressureView];
    [self.scrollView addSubview:self.rateView];
    [self.scrollView addSubview:self.weightView];
    [self.scrollView addSubview:self.alertView];
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.weightView.frame)+viewPix(30));
    [self requestBloodOxygenData:self.startDate7];
    [self requestPressureData:self.startDate7];
    [self requestRateData:self.startDate7];
    [self requestWeightData:self.startDate7];
   
}

//血氧数据
-(void)requestBloodOxygenData:(NSString *)startDate{
    [RequestUtil withGET:@"/record/list" parameters:@{@"patientId":self.patientid,@"startDay":startDate,@"endDay":self.endDate,@"markType":@"33"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.oxygenView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}
//血压
-(void)requestPressureData:(NSString *)startDate{
    [RequestUtil withGET:@"/record/list" parameters:@{@"patientId":self.patientid,@"startDay":startDate,@"endDay":self.endDate,@"markType":@"34"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.pressureView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}
//心率
-(void)requestRateData:(NSString *)startDate{
    [RequestUtil withGET:@"/record/list" parameters:@{@"patientId":self.patientid,@"startDay":startDate,@"endDay":self.endDate,@"markType":@"35"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.rateView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//体重
-(void)requestWeightData:(NSString *)startDate{
    [RequestUtil withGET:@"/record/list" parameters:@{@"patientId":self.patientid,@"startDay":startDate,@"endDay":self.endDate,@"markType":@"36"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.weightView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//血氧
-(void)oxygenDayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.oxygenView.dayButton7) {
        self.oxygenView.dayButton30.selected = NO;
        [self requestBloodOxygenData:self.startDate7];
    }else{
        self.oxygenView.dayButton7.selected = NO;
        [self requestBloodOxygenData:self.startDate30];
    }
}

//心率
-(void)rateDayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.rateView.dayButton7) {
        self.rateView.dayButton30.selected = NO;
        [self requestRateData:self.startDate7];
    }else{
        self.rateView.dayButton7.selected = NO;
        [self requestRateData:self.startDate30];
    }
}

//血压
-(void)pressureDayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.pressureView.dayButton7) {
        self.pressureView.dayButton30.selected = NO;
        [self requestPressureData:self.startDate7];
    }else{
        self.pressureView.dayButton7.selected = NO;
        [self requestPressureData:self.startDate30];
    }
}

//体重
-(void)weightDayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.weightView.dayButton7) {
        self.weightView.dayButton30.selected = NO;
        [self requestWeightData:self.startDate7];
    }else{
        self.weightView.dayButton7.selected = NO;
        [self requestWeightData:self.startDate30];
    }
}

-(void)showAlertView:(NSArray *)dataArray  date:(NSString *)dateString  point:(CGPoint)point{
    CGFloat marginX = (point.x>Screen_W/2.0)?(point.x-viewPix(140)-3):(point.x+3);
    self.alertView.frame = CGRectMake(marginX, point.y+3, viewPix(140), viewPix(35)+viewPix(24)*dataArray.count);
    self.alertView.dateString = dateString;
    self.alertView.dataArray = dataArray;
    self.alertView.hidden = NO;
    if(point.x>Screen_W/2.0){
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }else{
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners: UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }
}

-(void)chartViewTouchesBegan{
    self.alertView.hidden = YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self chartViewTouchesBegan];
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
        _scrollView.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
        _scrollView.contentSize = CGSizeMake(Screen_W, Screen_H);
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(LGBodayStatisticsOxygenView *)oxygenView{
    if(!_oxygenView){
        _oxygenView = [[LGBodayStatisticsOxygenView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(370))];
        [_oxygenView.dayButton7 addTarget:self action:@selector(oxygenDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_oxygenView.dayButton30 addTarget:self action:@selector(oxygenDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        _oxygenView.delegate = self;
    }
    return _oxygenView;
}

-(LGBodayStatisticsPressureView *)pressureView{
    if(!_pressureView){
        _pressureView = [[LGBodayStatisticsPressureView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.oxygenView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(415))];
        [_pressureView.dayButton7 addTarget:self action:@selector(pressureDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_pressureView.dayButton30 addTarget:self action:@selector(pressureDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _pressureView.delegate = self;
    }
    return _pressureView;
}

-(LGBodayStatisticsRateView *)rateView{
    if(!_rateView){
        _rateView = [[LGBodayStatisticsRateView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.pressureView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(370))];
        [_rateView.dayButton7 addTarget:self action:@selector(rateDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_rateView.dayButton30 addTarget:self action:@selector(rateDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _rateView.delegate = self;
    }
    return _rateView;
}

-(LGBodayStatisticsWeightView *)weightView{
    if(!_weightView){
        _weightView = [[LGBodayStatisticsWeightView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.rateView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(460))];
        [_weightView.dayButton7 addTarget:self action:@selector(weightDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_weightView.dayButton30 addTarget:self action:@selector(weightDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _weightView.delegate = self;
    }
    return _weightView;
}

-(LGChartDetailAlertView *)alertView{
    if(!_alertView){
        _alertView = [[LGChartDetailAlertView alloc] initWithFrame:CGRectMake(0, 0, viewPix(140), viewPix(50))];
        _alertView.hidden = YES;
    }
    return _alertView;
}


-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc]init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}

@end
